package com.hoppinzq.controller;

import com.hoppinzq.service.util.UUIDUtil;
import com.hoppinzq.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
public class CreateCodeController {
	
	/**生成代码
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value="/createCode")
	public void proCode(HttpServletResponse response) throws Exception{
		Map<String,Object> root = new HashMap<String,Object>();
		String uuid= UUIDUtil.getUUID();
		root.put("uuid", uuid);
		root.put("nowDate", new Date());//代码生成时间
		root.put("title","这是生成的SpringHoppinzq测试代码");//模块标题
		String modelName="test3";
		String upperModelName=modelName.substring(0,1).toUpperCase()+modelName.substring(1);
		root.put("modelName",modelName);//模块名称
		root.put("auther","hoppinzq");//作者
		//DelAllFile.delFolder(PathUtil.getClasspath()+"admin/ftl"); //生成代码前,先清空之前生成的代码
		root.put("enableBreak",true);//是否开启熔断器
		root.put("enableCore",true);//是否开启注册中心
		root.put("enableBalance",false);//是否开启负载均衡
		root.put("onlyClient",false);//是否只开启客户端？只开启客户端——》关闭注册中心，关闭熔断器
		root.put("javaVersion",11);//JDK版本号
		root.put("port","8888");//项目端口号

		String filePath = "admin/ftl/code/";//存放路径
		String ftlPath = "createCode";//ftl路径

		//生成启动类
		Freemarker.printFile("applicationTemplate.ftl", root, "hoppinzq-"+modelName+"/src/main/java/com/hoppinzq/"+upperModelName+"Application.java", filePath, ftlPath);
		//生成pom文件
		Freemarker.printFile("pomTemplate.ftl", root, "hoppinzq-"+modelName+"/pom.xml", filePath, ftlPath);
		//生成配置文件
		Freemarker.printFile("applicationYmlTemplate.ftl", root, "hoppinzq-"+modelName+"/src/main/resources/application.yml", filePath, ftlPath);
		Freemarker.printFile("applicationDevYmlTemplate.ftl", root, "hoppinzq-"+modelName+"/src/main/resources/application-dev.yml", filePath, ftlPath);
		Freemarker.printFile("applicationLocYmlTemplate.ftl", root, "hoppinzq-"+modelName+"/src/main/resources/application-loc.yml", filePath, ftlPath);
		//远程服务接口类
		Freemarker.printFile("interfaceTemplate.ftl", root, "hoppinzq-"+modelName+"/src/main/java/com/hoppinzq/service/TestService.java", filePath, ftlPath);
		Freemarker.printFile("demoInterfaceTemplate.ftl", root, "hoppinzq-"+modelName+"/src/main/java/com/hoppinzq/service/MyDemoService.java", filePath, ftlPath);
		Freemarker.printFile("demoServiceImplTemplate.ftl", root, "hoppinzq-"+modelName+"/src/main/java/com/hoppinzq/service/MyDemoServiceImpl.java", filePath, ftlPath);
		//远程服务bean类
		Freemarker.printFile("beanTemplate.ftl", root, "hoppinzq-"+modelName+"/src/main/java/com/hoppinzq/bean/TestBean.java", filePath, ftlPath);
		//controller层
		Freemarker.printFile("controllerTemplate.ftl", root, "hoppinzq-"+modelName+"/src/main/java/com/hoppinzq/controller/DemoController.java", filePath, ftlPath);
		//

		//Freemarker.printFile("docTemplate.ftl", root, "部署说明.doc", filePath, ftlPath);

		String os = System.getProperty("os.name").toLowerCase();
		if(os.contains("windows")) {
			if(FileZip.zip(PathUtil.getClasspath()+"admin/ftl/code", PathUtil.getClasspath()+"admin/ftl/code.zip")){
				FileDownload.fileDownload(response, PathUtil.getClasspath()+"admin/ftl/code.zip", "code.zip");
			}
		}else{
			if(FileZip.zip("/home/file/code/"+root.get("uuid"), "/home/file/code/"+root.get("uuid")+".zip")){
				FileDownload.fileDownload(response, "/home/file/code/"+root.get("uuid")+".zip", "code.zip");
			}
		}
	}
}
